package menu;

import java.io.File;
import java.util.HashMap;
import java.util.Scanner;

import batching.TextBatch;
import tf_idf.ReadFiles;

/**
 * 进行词频的计算*/
public class TF {
	static ReadFiles readfiles = new ReadFiles();
	static TextBatch textbatch = new TextBatch();
	static String TFofFile;
	
	/**
	 * 计算文档中的每个词的词频
	 * @param file
	 * @return
	 */
	
	public static String countTF(String file){
		String str=null;		
		StringBuffer stringbuffer = new StringBuffer(); 
		
		try{
		    String[] cutWordResult = readfiles.cutWord(file);
		    HashMap<String, Float> hashmap = readfiles.tf(cutWordResult);
		    
		    for(String strWord : hashmap.keySet()){
		    	float tf=hashmap.get(strWord);
		    	stringbuffer.append(strWord);
		        stringbuffer.append("\t");
		        stringbuffer.append(tf);
		        stringbuffer.append("\n");	
		    }
		}
		catch(Exception e){
		    	
		}
		str=stringbuffer.toString();
		TFofFile = str;
		return str;
	}
	
	/**
	 * 将词频写入另一个文档中
	 * @param file
	 */
	public static void writeTxt(String file){	
		textbatch.writeTxt(TFofFile,file);
	}
	
//	public static void main(String[] args){
//		String str = countTF("tf1");
//		//System.out.println(str);
//		writeTxt("E:tf1.txt");
//	}
}
